package com.test.languagetest;

import java.util.Set;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.pojo.Category;
import com.pojo.Product;

/**
 * @description 级联更新
 * @author Leon Plious
 * @date 2020年7月3日
 */
public class HibernateTestCascadeUpdateSave {

	/*
Hibernate: select category0_.id as id2_0_, category0_.name as name2_0_ from category_ category0_ where category0_.id=?
Hibernate: select products0_.cid as cid2_1_, products0_.id as id1_, products0_.id as id0_0_, products0_.name as name0_0_, products0_.price as price0_0_, products0_.cid as cid0_0_ from product_ products0_ where products0_.cid=?
Hibernate: select users0_.pid as pid0_1_, users0_.uid as uid1_, user1_.id as id3_0_ from user_product users0_ inner join user_ user1_ on users0_.uid=user1_.id where users0_.pid=?
Hibernate: insert into product_ (name, price, cid) values (?, ?, ?)
Hibernate: insert into product_ (name, price, cid) values (?, ?, ?)
Hibernate: insert into product_ (name, price, cid) values (?, ?, ?)
Hibernate: update product_ set cid=? where id=?
Hibernate: update product_ set cid=? where id=?
Hibernate: update product_ set cid=? where id=?
	 */
	public static void main(String[] args) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		session.beginTransaction();
		
		Category c = (Category) session.get(Category.class, 1);
		
		Set<Product> products = c.getProducts();
		Product product = new Product();
		product.setName("级联更新1");
		products.add(product);
		product = new Product();
		product.setName("级联更新2");
		products.add(product);
		product = new Product();
		product.setName("级联更新3");
		products.add(product);
		
		session.getTransaction().commit();
		session.close();
		sf.close();
		
	}
}
